#pragma once
// 文件版本
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <string>
#include <vector>
#include <cassert>

#include "../comm/log.hpp"
#include "../comm/util.hpp"

namespace ns_model
{
    using namespace std;
    using namespace ns_log;
    using namespace ns_util;

    struct Question
    {
        string number; // 题目编号，唯一的
        string name;   // 标题
        string star;   // 难度：简单、中等、困难
        int cpu_limit; // 时间要求(S)
        int mem_limit; // 空间要求(KB)

        string desc;   // 题目描述
        string header; // 为用户预设的代码
        string tail;   // 题目的测试用例，和header拼接形成完整代码
    };

    const string questions_list = "./questions/questions.list";
    const string question_path = "./questions/";
    const string blanksep = " ";

    class Model
    {
    private:
        // 题号 ： 题目细节
        unordered_map<string, Question> _questions;

    public:
        Model()
        {
            //?
            assert(LoadQuestionList(questions_list));
        }
        ~Model() {}
        // 把题库加载到内存中
        bool LoadQuestionList(const string &path)
        {
            ifstream in(path);
            if (!in.is_open())
            {
                LOG(FATAL) << "加载题库失败，检查是否存在题库文件" << endl;
                return false;
            }
            string line;
            while (getline(in, line))
            {
                vector<string> tokens;
                StringUtil::SplitString(line, &tokens, blanksep);
                if (tokens.size() != 5)
                {
                    LOG(WARNING) << "加载部分题目失败，请检查文件格式" << std::endl;
                    continue;
                }
                Question q;
                q.number = tokens[0];
                q.name = tokens[1];
                q.star = tokens[2];
                q.cpu_limit = stoi(tokens[3]);
                q.mem_limit = stoi(tokens[4]);

                string path = question_path + q.number + "/";
                FileUtil::ReadFile(path + "desc.txt", &(q.desc), true);
                FileUtil::ReadFile(path + "header.cpp", &(q.header), true);
                FileUtil::ReadFile(path + "tail.cpp", &(q.tail), true);

                _questions[q.number] = q;
                _questions.insert({q.number, q});
            }
            in.close();
            LOG(INFO) << "题库加载成功！" << std::endl;
            return true;
        }
        bool GetAllQuestions(vector<struct Question> *out)
        {
            if (_questions.size() == 0)
            {
                LOG(ERROR) << "用户获取题目列表失败" << endl;
                return false;
            }

            for (const auto &q : _questions)
            {
                out->push_back(q.second);
            }
            return true;
        }
        bool GetOneQuestion(const string &number, struct Question *q)
        {
            const auto &iter = _questions.find(number);
            if (iter == _questions.end())
            {
                LOG(ERROR) << "用户获取题目失败，题号：" << number << endl;
                return false;
            }
            (*q) = iter->second;
            return true;
        }
    };

} // namespace ns_model
